package initialize

import (
	"api/global"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
)

func InitNaCos() {
	res := global.AppData.NaCos
	clientConfig := constant.ClientConfig{
		NamespaceId:         res.Namespace, //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "debug",
	}
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      res.Host,
			ContextPath: "/nacos",
			Port:        res.Port,
			Scheme:      "http",
		},
	}

	configClient, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  &clientConfig,
			ServerConfigs: serverConfigs,
		},
	)
	if err != nil {
		panic(errors.New("链接nacos失败"))
	}
	content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: res.DataId,
		Group:  res.Group},
	)
	if err != nil {
		panic(errors.New("获取nacos配置信息失败"))
	}
	fmt.Println(content)
	json.Unmarshal([]byte(content), &global.AppData.Service)

}
